import asyncio
import logging
import time

from pyppeteer import launch

pyppeteer_level = logging.WARNING
logging.getLogger('pyppeteer').setLevel(pyppeteer_level)
logging.getLogger('websockets.protocol').setLevel(pyppeteer_level)
pyppeteer_logger = logging.getLogger('pyppeteer')
pyppeteer_logger.setLevel(logging.WARNING)

width = 1366
height = 768


async def main():
    browser = await launch(headless=False,
                           args=['--disable-infobars', f'--window-size={width},{height}', '--no-sandbox'], dumpio=True)
    page = await browser.newPage()

    # 设置页面大小
    await page.setViewport({"width": width, "height": height})
    await page.goto('https://demo.mycodes.net/daima/suopingjiesuo/')

    # 一些网站主要通过 window.navigator.webdriver 来对 webdriver 进行检测，所以我们只需要使用 JavaScript 将它设置为 false 即可
    await page.evaluate('''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')

    # # 截图
    # await page.screenshot({'path': '12306yzm.png',
    #                        "clip": {"x": (width * 1.25) / 2, "y": (height * 1.25) / 2 + 20, "width": width / 2,
    #                                 "height": height / 2}})

    # 获取盒子模型
    tar = await page.querySelector(".main-c")

    box = await tar.boundingBox()

    # 鼠标移动到第一个圈
    await page.mouse.move(box["x"] + 5, box["y"] + 5)  # # 鼠标移动
    time.sleep(1)
    # 鼠标按下
    await page.mouse.down()

    await page.mouse.move(box["x"] + 240, box["y"] + 240)
    time.sleep(1)
    await page.mouse.move(box["x"], box["y"] + 240)
    time.sleep(1)
    await page.mouse.move(box["x"] + 240, box["y"])
    time.sleep(1)
    # # # 鼠标放下
    #  876 434
    await page.mouse.up()
    #
    # #
    input("任意按键")
    await page.close()
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
